<?php

namespace App\Services\Applet\Sponsor;

use App\Exceptions\SponsorException;
use Exception;

class SponsorGoodsService extends BaseSponsorService
{
    /**
     * 商品列表
     * @param array $params
     * @return array
     * @throws Exception
     */
    public function list(array $params): array
    {
        // 获取商品列表
        $goodsList = $this->getSponsorGoodsModel()->list($params, [
            'id', 'category_id', 'province_id', 'city_id', 'region_id',
            'name', 'price', 'cover_img', 'start_time', 'cycle', 'params', 'show_sale_quantity', 'sale_quantity'
        ]);
        // 查询地区
        $provinceIds = array_column($goodsList, 'province_id');
        $cityIds = array_column($goodsList, 'city_id');
        $countyIds = array_column($goodsList, 'region_id');

        $areaIds = array_merge($provinceIds, $cityIds, $countyIds);
        // 查询地区
        $areaList = $this->getAreaModel()->getAreaNameListByCodeList($areaIds);
        $areaListMap = array_column($areaList, 'name', 'code');

        $list = [];
        foreach ($goodsList as $item) {
            //地区名称
            $areaName = '';
            !empty($areaListMap[$item['province_id']]) && $areaName .= $areaListMap[$item['province_id']];
            !empty($areaListMap[$item['city_id']]) && $areaName .= '-' . $areaListMap[$item['city_id']];
            !empty($areaListMap[$item['region_id']]) && $areaName .= '-' . $areaListMap[$item['region_id']];

            // 周期(周)
            $cycle = ceil($item['cycle'] / (7 * 24 * 60 * 60));

            $endDate = date('Y-m-d', strtotime($item['start_time']) + $item['cycle'] * (7 * 24 * 60 * 60));

            $list[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'areaName' => $areaName,
                'cycle' => $cycle,
                'endTime' => $endDate,
                'price' => $item['price'] / 100,
                'coverImg' => $item['cover_img'],
                'params' => json_decode($item['params'], true) ?? [],
                'showSaleQuantity' => $item['show_sale_quantity'],
                'saleQuantity' => $item['sale_quantity'],
            ];
        }

        return [
            'list' => $list
        ];
    }

    /**
     * 推荐商品列表
     * @return array
     * @throws Exception
     */
    public function recommendList(): array
    {
        // 获取商品列表
        $recommendGoodsList = $this->getSponsorGoodsModel()->recommendList([
            'id', 'category_id', 'province_id', 'city_id', 'region_id',
            'name', 'price', 'cover_img', 'start_time', 'cycle', 'params', 'show_sale_quantity', 'sale_quantity'
        ]);
        // 查询地区
        $provinceIds = array_column($recommendGoodsList, 'province_id');
        $cityIds = array_column($recommendGoodsList, 'city_id');
        $countyIds = array_column($recommendGoodsList, 'region_id');

        $areaIds = array_merge($provinceIds, $cityIds, $countyIds);
        // 查询地区
        $areaList = $this->getAreaModel()->getAreaNameListByCodeList($areaIds);
        $areaListMap = array_column($areaList, 'name', 'code');

        $list = [];
        foreach ($recommendGoodsList as $item) {
            //地区名称
            $areaName = '';
            !empty($areaListMap[$item['province_id']]) && $areaName .= $areaListMap[$item['province_id']];
            !empty($areaListMap[$item['city_id']]) && $areaName .= '-' . $areaListMap[$item['city_id']];
            !empty($areaListMap[$item['region_id']]) && $areaName .= '-' . $areaListMap[$item['region_id']];

            // 周期(周)
            $cycle = ceil($item['cycle'] / (7 * 24 * 60 * 60));

            $endDate = date('Y-m-d', strtotime($item['start_time']) + $item['cycle'] * (7 * 24 * 60 * 60));

            $list[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'areaName' => $areaName,
                'cycle' => $cycle,
                'endTime' => $endDate,
                'price' => $item['price'] / 100,
                'coverImg' => $item['cover_img'],
                'params' => json_decode($item['params'], true) ?? [],
                'showSaleQuantity' => $item['show_sale_quantity'],
                'saleQuantity' => $item['sale_quantity'],
            ];
        }

        return [
            'list' => $list
        ];
    }

    /**
     * 商品详情
     * @param array $params
     * @return array
     * @throws Exception
     */
    public function detail(array $params): array
    {

        $goodsDetail = $this->getSponsorGoodsModel()->getItemById($params['id'], [
            'id', 'category_id', 'province_id', 'city_id', 'region_id',
            'name', 'price', 'cover_img', 'start_time', 'cycle', 'params', 'status', 'show_sale_quantity', 'sale_quantity'
        ]);
        if (empty($goodsDetail)) {
            throw new SponsorException('商品不存在', SponsorException::CODE_1001);
        }

        $areaIds = [$goodsDetail['province_id'], $goodsDetail['city_id'], $goodsDetail['region_id']];
        // 查询地区
        $areaList = $this->getAreaModel()->getAreaNameByCode($areaIds);
        $areaNameList = array_column($areaList, 'name');
        $areaName = implode('-', $areaNameList);

        // 周期(周)
        $cycle = ceil($goodsDetail['cycle'] / (7 * 24 * 60 * 60));

        $endDate = date('Y-m-d', strtotime($goodsDetail['start_time']) + $goodsDetail['cycle'] * (7 * 24 * 60 * 60));

        return [
            'id' => $goodsDetail['id'],
            'name' => $goodsDetail['name'],
            'areaName' => $areaName,
            'cycle' => $cycle,
            'endTime' => $endDate,
            'price' => $goodsDetail['price'] / 100,
            'coverImg' => $goodsDetail['cover_img'],
            'params' => json_decode($goodsDetail['params'], true) ?? [],
            'showSaleQuantity' => $goodsDetail['show_sale_quantity'],
            'saleQuantity' => $goodsDetail['sale_quantity'],
            'status' => $goodsDetail['status'],
        ];
    }
}
